package com.luobo.home.controller;

import com.alibaba.fastjson.JSON;
import com.luobo.home.pojo.Items;
import com.luobo.home.pojo.MyRequest;
import com.luobo.home.pojo.RequestItem;
import com.luobo.home.service.ItemsService;
import com.luobo.home.utils.ListPojoToListMap;
import com.luobo.home.utils.ListToStringUtils;
import com.luobo.home.utils.MapToJsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.luobo.home.utils.ListPojoToListMap.createItemsMap;
import static jdk.nashorn.internal.objects.NativeString.substr;

@RestController
public class queryItems {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/api/queryitems")
    public String queryItems(){

        Map<String,Object> resultMap =  new HashMap<>();

        int queryCount = itemsService.queryCount();
        resultMap.put("code","000000");
        resultMap.put("msg","Success");
        resultMap.put("count",String.valueOf(queryCount));

        List<Items> items = itemsService.queryAllData();

        List<Map<String,Object>> itemsMap = ListPojoToListMap.createItemsMap(items);
        resultMap.put("data",itemsMap);

        String resultString = null;
        try {
            resultString = MapToJsonUtils.mapToStringJson(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }


    @RequestMapping("/api/queryitemsbyid/{itemsId}")
    public String queryItemsbyId(@PathVariable("itemsId") String itemsIdpara){
        Map<String,Object> resultMap =  new HashMap<>();

        Map<String,String> paraMap = new HashMap<>();
        paraMap.put("ItemId",itemsIdpara);

        int queryCount = itemsService.queryCountForMap(paraMap);

        if (queryCount != 0) {
            List<Items> itemsList = itemsService.queryDataForMap(paraMap);
            List<Map<String,Object>> itemsMap = ListPojoToListMap.createItemsMap(itemsList);
            resultMap.put("code","000000");
            resultMap.put("msg","Success");
            resultMap.put("count",String.valueOf(queryCount));
            resultMap.put("data",itemsMap);

        }else{
            resultMap.put("code","000001");
            resultMap.put("msg","data is null");
            resultMap.put("count",String.valueOf(queryCount));
            resultMap.put("data",null);
        }

        String resultString = null;
        try {
            resultString = MapToJsonUtils.mapToStringJson(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    @PostMapping("/api/queryitemsbypost")
    private ResponseEntity<String> queryitemsbypost(@RequestBody Items items) {

        Map<String,Object> resultMap =  new HashMap<>();
        List<Map<String,Object>> itemsListMap = null;
        Map<String, String> paraMap = new HashMap<>();
        System.out.println(items.toString());
        if ( items.getItemName() != null) {
            paraMap.put("itemName", items.getItemName());
        }
        if ( items.getItemCategroy() != null) {
            paraMap.put("itemCategroy", items.getItemCategroy());
        }
        if ( items.getItemPosition() != null) {
            paraMap.put("itemPosition", items.getItemPosition());
        }
        if ( items.getItemUse() != null) {
            paraMap.put("itemUse", items.getItemUse());
        }
        if ( items.getItemOvertime() != null) {
            paraMap.put("itemOvertime", substr(items.getItemOvertime(),0,23));
        }
        System.out.println(paraMap.toString());

        if ( !paraMap.isEmpty() ) {
            int queryCount = itemsService.queryCountForMap(paraMap);

            if (queryCount != 0) {
                List<Items> itemsList = itemsService.queryDataForMap(paraMap);
                itemsListMap =  ListPojoToListMap.createItemsMap(itemsList);
                resultMap.put("code","000000");
                resultMap.put("success",true);
                resultMap.put("msg","Success");
                resultMap.put("count",String.valueOf(queryCount));
                resultMap.put("data",itemsListMap);

            }else{
                resultMap.put("code","000002");
                resultMap.put("msg","itemsName:" + paraMap.get("itemName") + "querydb get result is null");
                resultMap.put("count",String.valueOf(queryCount));
                resultMap.put("data",null);

            }
        }else{
            int queryCount = itemsService.queryCount();

            List<Items> itemsList = itemsService.queryAllData();
            itemsListMap =  ListPojoToListMap.createItemsMap(itemsList);
            resultMap.put("code","000001");
            resultMap.put("msg","itemName is null,result is allData");
            resultMap.put("count",String.valueOf(queryCount));
            resultMap.put("data",itemsListMap);
        }


        String responseString = null;
        try {
            responseString = MapToJsonUtils.mapToStringJson(resultMap);
//            responseString = JSON.toJSONString(itemsListMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(responseString);
    }



    @PostMapping("/api/addItems")
        public ResponseEntity<String> addItems(@RequestBody RequestItem requestItems){
        Map<String,Object> resultMap =  new HashMap<>();
        System.out.println(requestItems.toString());
        Items items = new Items(requestItems.getItemId(),requestItems.getItemName(), ListToStringUtils.ListToString(requestItems.getItemUrllist())
                ,requestItems.getItemAmount(),substr(requestItems.getItemOvertime(),0,23),requestItems.getItemStatus()
                ,requestItems.getItemCategroy(),requestItems.getItemUse(),requestItems.getItemPosition()
                , ListToStringUtils.ListToString(requestItems.getPositionUrllist()),requestItems.getRemark());

        int addreturn = itemsService.addData(items);

        if (addreturn != -1){
            resultMap.put("code","000000");
            resultMap.put("msg","Success:add num:"+addreturn);
            resultMap.put("success",true);

        }else{
            resultMap.put("code","999999");
            resultMap.put("msg","fail:add num:"+addreturn);
            resultMap.put("success",false);
        }



        String responseString = null;
        try {
            responseString = MapToJsonUtils.mapToStringJson(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(responseString);
    }


    @RequestMapping("/api/deleteItemsbyId/{itemsId}")
    public String deleteItemsbyId(@PathVariable("itemsId") String itemsIdpara){

        Map<String,Object> resultMap =  new HashMap<>();
        Map<String,String> paraMap = new HashMap<>();
        paraMap.put("ItemId",itemsIdpara);

        int queryCount = itemsService.queryCountForMap(paraMap);

        if (queryCount != 0) {
            int deleteResult = itemsService.delDataForMap(paraMap);
            resultMap.put("code","000000");
            resultMap.put("msg","Success");
            resultMap.put("count",String.valueOf(queryCount));
            resultMap.put("data",String.valueOf(deleteResult));

        }else{
            resultMap.put("code","000001");
            resultMap.put("msg","data is null");
            resultMap.put("count",String.valueOf(queryCount));
            resultMap.put("data",null);
        }

        String resultString = null;
        try {
            resultString = MapToJsonUtils.mapToStringJson(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    @PostMapping("/api/updateItemsbyId")
    public ResponseEntity<String> updateItemsbyId(@RequestBody RequestItem requestItems){
            Map<String,Object> resultMap =  new HashMap<>();
            System.out.println(requestItems.toString());
            Items items = new Items(requestItems.getItemId(),requestItems.getItemName(), ListToStringUtils.ListToString(requestItems.getItemUrllist())
                    ,requestItems.getItemAmount(),substr(requestItems.getItemOvertime(),0,23),requestItems.getItemStatus()
                    ,requestItems.getItemCategroy(),requestItems.getItemUse(),requestItems.getItemPosition()
                    , ListToStringUtils.ListToString(requestItems.getPositionUrllist()),requestItems.getRemark());

            int addreturn = itemsService.updateDatabyId(items);

            if (addreturn != -1){
                resultMap.put("code","000000");
                resultMap.put("msg","Success:add num:"+addreturn);
                resultMap.put("success",true);

            }else{
                resultMap.put("code","999999");
                resultMap.put("msg","fail:add num:"+addreturn);
                resultMap.put("success",false);
            }



            String responseString = null;
            try {
                responseString = MapToJsonUtils.mapToStringJson(resultMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseEntity.ok(responseString);
    }
}
